﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Data.OleDb
Imports Transitions
Imports System.Data.DataTable
Imports WiseLibraries
Public Class CaixaRegistradora
    Inherits KryptonForm
    Private conexao As OleDbConnection
    Private dataSet As DataSet
    Private dataAdapter As OleDbDataAdapter
    Private cmd As OleDbCommand
    Private cmdcx As OleDbCommand
    Dim motivosaida As String
    Dim qtdeEntrada, qtdeSaida, retirado As Double
    Dim total As Double
    Dim sqlretira As String
    Dim dtsaida As Date
    Dim usuario As Usuario = Login.usuario

    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        conexao = FabricaDeConexoes.CriarConexao
        carregarvalor()
    End Sub

    Private Sub carregarvalor()
        Dim estado As String = Nothing
        dataSet = New DataSet()

        Try
            conexao.Open()

            dataAdapter = New OleDbDataAdapter("SELECT SUM(valorentradaCaixa) AS entrada FROM tblentradaCaixa", conexao)
            dataAdapter.Fill(dataSet, "tblentradaCaixa")
            qtdeEntrada = If(dataSet.Tables("tblentradaCaixa").Rows(0).Item("entrada").Equals(DBNull.Value), 0, dataSet.Tables("tblentradaCaixa").Rows(0).Item("entrada"))

            dataAdapter = New OleDbDataAdapter("SELECT SUM(valorsaidaCaixa) AS saida FROM tblSaidaCaixa", conexao)
            dataAdapter.Fill(dataSet, "tblSaidaCaixa")
            qtdeSaida = If(dataSet.Tables("tblSaidaCaixa").Rows(0).Item("saida").Equals(DBNull.Value), 0, dataSet.Tables("tblSaidaCaixa").Rows(0).Item("saida"))

            total = qtdeEntrada - qtdeSaida
            lblvalor.Text = String.Format("R$ {0:0.00}", total)
            Dim sqlCaixa As String = "UPDATE tblCaixa SET valor = " & total & " WHERE aberto = 'sim'"
            cmdcx = New OleDbCommand(sqlCaixa, conexao)
            cmdcx.ExecuteNonQuery()
            Debug.WriteLine(sqlCaixa)

        Catch ex As Exception
            Debug.WriteLine(ex)
        Finally
            conexao.Close()
        End Try
    End Sub

    Private Sub retirarvalor()
        If (String.IsNullOrEmpty(txtretirarquantia.Text) Or String.IsNullOrEmpty(txtmotivo.Text)) Then

            KryptonMessageBox.Show("Preencha a Quantia e o Motivo da Retirada!", "Atenção")
        Else

            retirado = txtretirarquantia.GetDoubleValue()
            dtsaida = CDate(DateTime.Now.ToString("dd/MM/yyyy"))
            Debug.WriteLine(retirado)
            Debug.WriteLine(dtsaida)
            motivosaida = txtmotivo.Text
            sqlretira = "INSERT INTO tblSaidaCaixa(valorsaidaCaixa, motivosaidaCaixa, dtsaidaCaixa, codLogin)VALUES(" & retirado & ",'" & motivosaida & "','" & dtsaida & "'," & usuario.Codigo & ")"

            If (retirado > total) Then

                KryptonMessageBox.Show("Você não tem todo este dinheiro em Caixa!!!", "Atenção")
                txtretirarquantia.Clear()
            ElseIf (retirado <= total) Then

                If (KryptonMessageBox.Show("Deseja realmente retirar R$" & retirado & " do Caixa?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then

                    Try
                        conexao.Open()
                        cmd = New OleDbCommand(sqlretira, conexao)
                        cmd.ExecuteNonQuery()

                        KryptonMessageBox.Show("Quantia retirada: R$" & retirado, "Atenção")
                        txtmotivo.Clear()
                        txtretirarquantia.Clear()

                    Catch ex As Exception
                        Debug.WriteLine(ex)
                    Finally
                        conexao.Close()
                    End Try
                Else
                    KryptonMessageBox.Show("Trasação cancelada!", "Cancelado")
                    txtmotivo.Clear()
                    txtretirarquantia.Clear()
                End If
            End If
        End If
        carregarvalor()
    End Sub

    Private Sub btnretirar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnretirar.Click
        retirarvalor()
    End Sub

    Private Sub btnAbrirFechar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbrirFechar.Click
        Me.Dispose()
    End Sub
End Class